#include <kungfu/yijinjing/io.h>
#include <kungfu/yijinjing/common.h>
#include <kungfu/yijinjing/log/setup.h>

#include <kungfu/practice/apprentice.h>
#include <kungfu/wingchun/book/book.h>
#include <kungfu/wingchun/service/ledger.h>

#include "mint/service/mlocator.h"
#include "mint/service/mbook.h"
#include "mint/data/sqlite/book_proxy.h"
#include "mint/data/sqlite/location_proxy.h"

using namespace kungfu::wingchun::msg;
using namespace kungfu::wingchun::book;
using namespace kungfu::wingchun::mapper;
using namespace kungfu::yijinjing::data;
using namespace junyi::mint::wrapper;

namespace junyi
{
    namespace mint
    {
        namespace wrapper
        {
            class MLedger : public kungfu::wingchun::service::Ledger
            {
            public:
              MLedger(std::shared_ptr<MLocator> locator, mode m, bool low_latency = false);

              ~MLedger() {
              };

              std::string handle_request(const kungfu::yijinjing::event_ptr &event, const std::string &msg) override;

              void handle_instrument_request(const kungfu::yijinjing::event_ptr &event) override;

              void handle_asset_request(const kungfu::yijinjing::event_ptr &event, const location_ptr &app_location) override;

              void on_app_location(int64_t trigger_time, const location_ptr &app_location) override;

              void on_quote(kungfu::yijinjing::event_ptr event, const kungfu::wingchun::msg::data::Quote& quote) override;

              void on_order(kungfu::yijinjing::event_ptr event, const kungfu::wingchun::msg::data::Order &order) override;

              void on_trade(kungfu::yijinjing::event_ptr event, const kungfu::wingchun::msg::data::Trade &trade) override;

              void on_instruments(const std::vector<kungfu::wingchun::msg::data::Instrument> &instruments) override;

              void pre_start() override;

              void asset_snapshot(std::string data_frequency = "minute");
            private:
                std::shared_ptr<BookOrderMapper> book_order_mapper_;

                std::shared_ptr<BookPositionMapper> book_position_mapper_;

                std::shared_ptr<BookAssetMapper> book_asset_mapper_;

                std::shared_ptr<LocationMapper> location_mapper_;

                void mark_orders_status_unknown(std::list<kungfu::wingchun::msg::data::Order>& book_orders);

                std::shared_ptr<MBook> make_book_info(const location_ptr& app_location);
            };
        }
    }
}